package com.ktjy.controller;

import com.ktjy.entity.User;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class HelloController {

    @RequestMapping("/hello")
    public String hello(Model model, HttpSession session){
        model.addAttribute("msg","welcome thymeleaf");

        //在session中存储一个数据
        session.setAttribute("username","zhangsan");

        model.addAttribute("date",new Date());

        User user=new User();
        user.setName("lisi");
        user.setAge(18);
        user.setSex("男");
        model.addAttribute("user",user);
        return "hello";
    }

    @RequestMapping("/link")
    public String link(){
        return "link";
    }

    @RequestMapping("/add")
    @ResponseBody
    public User add(User user){
        return user;
    }

    @RequestMapping("/properties")
    public String properties(Model model){
        model.addAttribute("method","post");
        model.addAttribute("name","username");
        model.addAttribute("value","zhangsan");
        model.addAttribute("color","color:red");
        return "properties";
    }

    @RequestMapping("/ifunless")
    public String ifunless(Model model){
        model.addAttribute("age",20);
        model.addAttribute("sex","人妖");
        return "ifunless";
    }

    @RequestMapping("/each")
    public String each(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User("张三",18,"男"));
        list.add(new User("李四",19,"女"));
        list.add(new User("王五",20,"男"));

        model.addAttribute("list",list);

        return "each";
    }

    @RequestMapping("/frame")
    public String frame(){
        return "frame";
    }

    @RequestMapping("/userlist")
    public String userList(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User("张三2",18,"男"));
        list.add(new User("李四2",19,"女"));
        list.add(new User("王五2",20,"男"));

        model.addAttribute("list",list);

        return "userlist";
    }
}
